package pct.page;

import java.util.LinkedList;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import pct.database.PcServiceImpl;
import pct.entity.Group;
import pct.entity.Post;
import pct.entity.Topic;
import pct.manage.StatusManager;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings({ "serial"})
public class NextPage extends ActionSupport implements
		Preparable, SessionAware {
	private PcServiceImpl pcService = new PcServiceImpl();
	private Map<String, Object> session;
	private String type;
	private String target;
	
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String execute() {
		if(type.equals("1"))
		{
			StatusManager sm = (StatusManager) session.get("status");
			PageFilter pf = new PageFilter();
			int currentPageNumber = sm.getCurrentPage();
			sm.setPageSize(pf.getGroupPages(sm.getCurrentUser()));
			switch(sm.getPageType()) {
			case 1:
				// viewing groups
				int pageSize = pf.getGroupPages(sm.getCurrentUser());
				if(currentPageNumber+1 <= pageSize){
					LinkedList<Group> relatedGroups = pf.getTargetedPageGroups(sm.getCurrentUser(), currentPageNumber+1);
					if (relatedGroups != null) {
						for (Group i : relatedGroups) {
							i.setTopicNumber(pcService.retrieveTopicNumber(i
									.getGroupName()));
							i.setPostNumber(pcService.retrievePostNumber(i
									.getGroupName()));
						}
					}
					sm.setRelatedGroups(relatedGroups);
					sm.setCurrentPage(currentPageNumber+1);
				}
				else{
					LinkedList<Group> relatedGroups = pf.getFirstPageGroups(sm.getCurrentUser());
					if (relatedGroups != null) {
						for (Group i : relatedGroups) {
							i.setTopicNumber(pcService.retrieveTopicNumber(i
									.getGroupName()));
							i.setPostNumber(pcService.retrievePostNumber(i
									.getGroupName()));
						}
					}
					sm.setRelatedGroups(relatedGroups);
					sm.setCurrentPage(1);
				}
				break;
			case 2:
				// viewing topics
				int pageSize_2 = pf.getTopicPages(sm.getCurrentGroupName());
				if(currentPageNumber+1 <= pageSize_2) {
					LinkedList<Topic> relatedTopics = pf.getTargetedPageTopics(sm.getCurrentGroupName(), currentPageNumber+1);
					if(relatedTopics!=null)
					{
						for(Topic i : relatedTopics) {
							i.setPostNumber(pcService.retrievePostNumber(i.getTopicID()));
						}
					}
					sm.setRelatedTopics(relatedTopics);
					sm.setCurrentPage(currentPageNumber+1);
				}
				else {
					LinkedList<Topic> relatedTopics = pf.getFirstPageTopics(sm.getCurrentGroupName());
					if(relatedTopics!=null)
					{
						for(Topic i : relatedTopics) {
							i.setPostNumber(pcService.retrievePostNumber(i.getTopicID()));
						}
					}
					sm.setRelatedTopics(relatedTopics);
					sm.setCurrentPage(1);
				}
				break;
			case 3:
				break;
			case 11:
				break;
			case 12:
				// viewing topics by search
				
				break;
			case 13:
				break;
			}
			session.put("status", sm);
			return "home";
		}
		else if(type.equals("2"))
		{
			{
				StatusManager sm = (StatusManager) session.get("status");
				PageFilter pf = new PageFilter();
				int currentPageNumber = sm.getCurrentPage();
				sm.setPageSize(pf.getSearchGroupPages(target));
				switch(sm.getPageType()) {
				case 4:
					// viewing groups
					int pageSize = pf.getSearchGroupPages(target);
					if(currentPageNumber+1 <= pageSize){
						LinkedList<Group>  relatedGroups = pf.getPageMatchedGroups(target, currentPageNumber+1);
						if (relatedGroups != null) {
							for (Group i :  relatedGroups) {
								i.setTopicNumber(pcService.retrieveTopicNumber(i
										.getGroupName()));
								i.setPostNumber(pcService.retrievePostNumber(i
										.getGroupName()));
							}
						}
						sm.setSearchedGroups( relatedGroups);
						sm.setCurrentPage(currentPageNumber+1);
					}
					else{
						LinkedList<Group>  relatedGroups = pf.getFirstPageMatchedGroups(target);
						if ( relatedGroups != null) {
							for (Group i :  relatedGroups) {
								i.setTopicNumber(pcService.retrieveTopicNumber(i
										.getGroupName()));
								i.setPostNumber(pcService.retrievePostNumber(i
										.getGroupName()));
							}
						}
						sm.setSearchedGroups(relatedGroups);
						sm.setCurrentPage(1);
					}
					break;
				case 5:
					// viewing topics
					int pageSize_2 = pf.getSearchTopicPages(target);
					if(currentPageNumber+1 <= pageSize_2) {
						LinkedList<Topic> relatedTopics = pf.getPageMatchedTopics(target, currentPageNumber+1);
						if(relatedTopics!=null)
						{
							for(Topic i : relatedTopics) {
								i.setPostNumber(pcService.retrievePostNumber(i.getTopicID()));
							}
						}
						sm.setSearchedTopics(relatedTopics);
						sm.setCurrentPage(currentPageNumber+1);
					}
					else {
						LinkedList<Topic> relatedTopics = pf.getFirstPageMatchedTopics(target);
						if(relatedTopics!=null)
						{
							for(Topic i : relatedTopics) {
								i.setPostNumber(pcService.retrievePostNumber(i.getTopicID()));
							}
						}
						sm.setSearchedTopics(relatedTopics);
						sm.setCurrentPage(1);
					}
					break;
				case 6:
					int pageSize_3 = pf.getSearchPostPages(target);
					if(currentPageNumber+1 <= pageSize_3){
						LinkedList<Post>  relatedPosts = pf.getPageMatchedPosts(target, currentPageNumber+1);
						if (relatedPosts != null) {
							for (Post i :  relatedPosts) {
								i.setUserID(i.getUserID());
								i.setPostTime(i.getPostTime());
								i.setGroupName(pcService.retrieveGroupName(i.getTopicID()));
								i.setTitle(pcService.retrieveTitle(i.getTopicID()));
							}
						}
						sm.setSearchedPosts( relatedPosts);
						sm.setCurrentPage(currentPageNumber+1);
					}
					else{
						LinkedList<Post>  relatedPosts = pf.getFirstPageMatchedPosts(target);
						if ( relatedPosts != null) {
							for (Post i :  relatedPosts) {
								i.setUserID(i.getUserID());
								i.setPostTime(i.getPostTime());
								i.setGroupName(pcService.retrieveGroupName(i.getTopicID()));
								i.setTitle(pcService.retrieveTitle(i.getTopicID()));
							}
						}
						sm.setSearchedPosts(relatedPosts);
						sm.setCurrentPage(1);
					}
					break;
				case 11:
					break;
				case 12:
					// viewing topics by search
					
					break;
				case 13:
					break;
				}
				session.put("status", sm);
				return "search";
			}
		}
		else
			return SUCCESS;
	}
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void prepare() throws Exception {
		pcService.connectDB();
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
}
